import { Button, Space } from 'ant-design-vue'
import { sheLfEnum, tableColumns } from './data'

export function useColumns(updateFunction: Function, detailFunction: Function, updateStatusFunction: Function) {
  const getColumns = (column, record) => {
    if (column.dataIndex === 'name') {
      return <span>{record.name}</span>
    } else if (column.dataIndex === 'brandProviderName') {
      return <span>{record.brandProviderName}</span>
    } else if (column.dataIndex === 'productUniqueCode') {
      return <span>{record.productUniqueCode}</span>
    } else if (column.dataIndex === 'specificationNumber') {
      return (
        <span>
          {record.specificationNumber}
          {record.specificationName}
        </span>
      )
    } else if (column.dataIndex === 'packingNumber') {
      return (
        <span>
          {record.packingNumber}
          {record.packingName}
        </span>
      )
    } else if (column.dataIndex === 'shelfLife') {
      let shelfLiftUnitStr = ''
      sheLfEnum.forEach(model => {
        if (model.value == record.shelfLifeUnit) {
          shelfLiftUnitStr = model.label
        }
      })
      return (
        <span>
          {record.shelfLife}
          {shelfLiftUnitStr}
        </span>
      )
    } else if (column.dataIndex === 'originalPrice') {
      return <span>{record.originalPrice}元</span>
    } else if (column.dataIndex === 'supplyPrice') {
      return <span>{record.supplyPrice}元</span>
    } else if (column.dataIndex === 'status') {
      if (record.status === 0) {
        return <span style={{ color: 'grey' }}>&bull; 禁用</span>
      }
      return <span style={{ color: 'blue' }}>&bull; 正常</span>
    } else if (column.dataIndex === 'createTime') {
      return <span>{record.createTime}</span>
    } else if (column.key === 'action') {
      return (
        <Space>
          <Button type='link' onClick={() => detailFunction(record.code)} v-auth={['base:shop:detail']}>
            详情
          </Button>
          <Button type='link' onClick={() => updateFunction(record.code)} v-auth={['base:shop:edit']}>
            编辑
          </Button>
          <Button
            type='link'
            onClick={() => updateStatusFunction(record.code, record.status === 0 ? 1 : 0)}
            v-auth={['base:shop:status']}
          >
            {record.status === 0 ? '启用' : '禁用'}
          </Button>
        </Space>
      )
    }
  }
  return {
    getColumns,
    tableColumns
  }
}
